package com.mars.common.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 业务异常基类
 * <p>所有业务异常都必须继承于此异常，定义异常时，需要先确定异常所属模块。例如：添加商户报错 可以定义为 [10020001]前四位数为系统模块编号，后4位为错误代码 ,唯一
 * 后台运维异常 1002<br>
 * 会员门户异常 1003<br>
 * boss门户异常 1004<br>
 * 会计门户异常 1005<br>
 * 通知应用异常 1006<br>
 * 
 * @author yhaoquan
 * 
 */
public class BizException extends AppException {

	private static final long serialVersionUID = 1L;

	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * 数据库操作,insert返回0
	 */
	public static final BizException DB_INSERT_RESULT_0 = new BizException(
			90040010, "数据库操作,insert返回0");

	/**
	 * 数据库操作,batch insert异常
	 */
	public static final BizException DB_BATCH_INSERT_RESULT_ERROR = new BizException(
			90040011, "数据库操作,batch insert异常");

	/**
	 * 数据库操作,insert or update异常
	 */
	public static final BizException DB_INSERT_OR_UPDATE_RESULT_ERROR = new BizException(
			90040012, "数据库操作,insert or update异常");

	/**
	 * 数据库操作,delete返回0
	 */
	public static final BizException DB_DELETE_RESULT_0 = new BizException(
			90040020, "数据库操作,delete返回0");

	/**
	 * 数据库操作,update返回0
	 */
	public static final BizException DB_UPDATE_RESULT_0 = new BizException(
			90040030, "数据库操作,update返回0");

	/**
	 * 数据库操作,count异常
	 */
	public static final BizException DB_COUNT_ERROR = new BizException(
			90040040, "数据库操作,count异常");

	/**
	 * 数据库操作,selectOne返回null
	 */
	public static final BizException DB_SELECTONE_IS_NULL = new BizException(
			90040050, "数据库操作,selectOne返回null");

	/**
	 * 数据库操作,list返回null
	 */
	public static final BizException DB_LIST_IS_NULL = new BizException(
			90040051, "数据库操作,list返回null");

	/**
	 * 数据库操作,分页查询异常
	 */
	public static final BizException DB_SELECT_PAGE = new BizException(
			90040052, "数据库操作,分页查询异常");

	/**
	 * 判断字段值是否存在,发生异常
	 */
	public static final BizException DB_CHECK_VALUE = new BizException(
			90040052, "判断字段值是否存在,发生异常");

	/**
	 * 获取序列出错
	 */
	public static final BizException DB_GET_SEQ_NEXT_VALUE_ERROR = new BizException(
			90040007, "获取序列出错");

	/**
	 * 异常信息
	 */
	protected String msg;

	/**
	 * 具体异常码
	 */
	protected int code;

	public BizException(String msgFormat, Object... args) {
		super(String.format(msgFormat, args));
		this.msg = String.format(msgFormat, args);
	}

	public BizException(int code, String msgFormat, Object... args) {
		super(String.format(msgFormat, args));
		this.code = code;
		this.msg = String.format(msgFormat, args);
	}

	public BizException logInfo() {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < 20; i++) {
			sb.append("=========");
		}
		logger.info("\r\n" + sb.toString() + "\r\n"
				+ "====>BizException, code:" + this.code + ", msg:" + this.msg,
				"\r\n" + sb.toString() + "\r\n");
		return new BizException(this.code, this.msg);
	}

	public BizException logInfo(String exMsg) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < 20; i++) {
			sb.append("=========");
		}
		logger.info("\r\n" + sb.toString() + "\r\n"
				+ "====>BizException, code:" + this.code + ", msg:" + this.msg
				+ "  {} {}", exMsg, "\r\n" + sb.toString() + "\r\n");
		return new BizException(this.code, this.msg);
	}

	public BizException logInfo(Exception e) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < 20; i++) {
			sb.append("=========");
		}
		logger.info("\r\n" + sb.toString() + "\r\n"
				+ "====>BizException, code:" + this.code + ", msg:" + this.msg
				+ "  {} {}", e.getMessage(), "\r\n" + sb.toString() + "\r\n");
		return new BizException(this.code, this.msg);
	}

	public BizException logError() {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < 20; i++) {
			sb.append("=========");
		}
		logger.error("\r\n" + sb.toString() + "\r\n"
				+ "====>BizException, code:" + this.code + ", msg:" + this.msg,
				"\r\n" + sb.toString() + "\r\n");
		return new BizException(this.code, this.msg);
	}

	public BizException logError(String exMsg) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < 20; i++) {
			sb.append("=========");
		}
		logger.error("\r\n" + sb.toString() + "\r\n"
				+ "====>BizException, code:" + this.code + ", msg:" + this.msg
				+ "  {} {}", exMsg, "\r\n" + sb.toString() + "\r\n");
		return new BizException(this.code, this.msg);
	}

	public BizException logError(Exception e) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < 20; i++) {
			sb.append("=========");
		}
		logger.error("\r\n" + sb.toString() + "\r\n"
				+ "====>BizException, code:" + this.code + ", msg:" + this.msg
				+ "  {} {}", e.getMessage(), "\r\n" + sb.toString() + "\r\n");
		return new BizException(this.code, this.msg);
	}

	public BizException() {
		super();
	}

	public BizException(String message, Throwable cause) {
		super(message, cause);
	}

	public BizException(String message) {
		super(message);
	}

	public BizException(Throwable cause) {
		super(cause);
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

}
